
// editable page setup
new EventListener(window, "load", function(evn) {
		var doc = Doc(document);
		var toggleImg = document.createElement("img");
		toggleImg.className = "editModeToggle";
		toggleImg.src = BasePath.get() + EDIT_IMAGE_BASE + "/readMode.gif";
		Dom(toggleImg).setOpacity(0.1);
		document.body.appendChild(toggleImg);
		doc.editModeImage = toggleImg;

		new EventListener(toggleImg, "click", function(evn) {
				if ((evn.button > 1) || (evn.type == "contextmenu"))
					onSystemContextMenu(evn);
				else
					toggleEditMode();
				return EventListener.cancelEvent(evn);
			});

		doc.addEventListener("mouseout", function(evn) {
				this.zoomEditToggle(1000);
			});

		doc.addEventListener("mousemove", function(evn) {
				this.zoomEditToggle((evn.clientX > evn.clientY)?evn.clientX:evn.clientY);
			});

		doc.addEventListener("mouseover", function(evn) {
				this.zoomEditToggle((evn.clientX > evn.clientY)?evn.clientX:evn.clientY);
			});

		new EventListener(toggleImg, "contextmenu", onSystemContextMenu);

		doc.addEventListener("keydown", function(evn) {
				if (evn.keyCode == 113) // F2
					toggleEditMode();
				else if (evn.keyCode == 27) // ESC
					this.systemContextMenu && this.systemContextMenu.hide();
			});

		doc.addEventListener("contextmenu", function(evn) {
				this._rightClicked = true;
				window.setTimeout(function(doc) {
						doc._rightClicked = false;
					}, 500, this);
			});

		doc.addEventListener("click", function(evn) {
				if (evn.button == 2) {
					this._rightClicked = true;
					window.setTimeout(function(doc) {
							doc._rightClicked = false;
						}, 500, this);
				}
				else if (this._rightClicked) {
					toggleEditMode();
				}
			});

		doc.addEventListener("dblclick", function(evn) {
				if ((this._rightClicked || evn.ctrlKey) && (evn.button < 2)) {
					try {
						if (document.selection)
							document.selection.empty();
						if (window.getSelection)
							window.getSelection().removeAllRanges();
					}
					catch (ex) {
					}
					toggleEditMode();
				}
			});

		doc.editModeCookie = new Cookie("editMode");
		if ("1" == doc.editModeCookie.getValue())
			toggleEditMode();
	});

function toggleEditMode() {
	scriptSupport.loadScript("Entity.js");
	scriptSupport.loadScript("EntityView.js");
	scriptSupport.loadScript("editActions.js");
	scriptSupport.loadScript("EditorPanel.js");
	scriptSupport.loadScript("EditJob.js");
	scriptSupport.loadScript("Selection.js");
	scriptSupport.loadScript("edit.js");
	// scriptSupport.loadScript("debug.js");
	scriptSupport.init();
}

function _toggleEditMode() {
	scriptSupport.loadScript("scriptsEditMode.js");
	scriptSupport.init();
}

// System Menu

function onSystemContextMenu(evn) {
	Doc().showSystemContextMenu(evn.clientX, evn.clientY, evn);
	return EventListener.cancelEvent(evn);
}

E.Doc.prototype.getMainEntityId = function() {
	if (this.mainEntityId)
		return this.mainEntityId;
	if (this.contentPanel)
		return this.contentPanel.element.id;
	return null;
}

E.Doc.prototype.showSystemContextMenu = function(x, y, evn) {
	if (0 > x) {
		x = this.editModeImage.offsetLeft;
		y = this.editModeImage.offsetTop;
	}
	this.systemContextMenu = new ContextPanel();
	this.systemContextMenu.show(this.editModeImage, x, y, evn);
	this.systemContextMenu.dom.style.position = "fixed";
	var objId = this.getMainEntityId();
	if (objId && (objId.indexOf("#") >= 0))
		objId = objId.substring(0, objId.indexOf("#"));
	var cm = new Menu();
	cm.add(new Menu(new ViewHistoryAction(objId)));
	cm.add(new Menu(new ViewExtHistoryAction(objId)));
	cm.add(new Menu(new EditViewAction(this.doc.documentElement.id)));
	var a = new ViewCosmosBrowserAction(objId);
	cm.add(new Menu(a));
	this.systemContextMenu.setContent(cm);
	this.systemContextMenu.onHide = function() {
		this.doc.zoomEditToggle();
	}
}

E.Doc.prototype.zoomEditToggle = function(mouseDistance) {
	if (mouseDistance)
		this.editModeImageMouseDistance = mouseDistance;
	var s = 100;
	if (!this.inEditMode && (!this.systemContextMenu || !this.systemContextMenu.isVisible())) {
		s = 100 - this.editModeImageMouseDistance;
		if (s < 1)
			s = 1;
	}
	var f = s;
	if (s > 36)
		s = 36;
	this.editModeImage.style.width = s + "px";
	this.editModeImage.style.height = s + "px";
	Dom(this.editModeImage).setOpacity(f / 100);
}

function ViewHistoryAction(objId) {
	this.id = "viewHistory";
	this.label = "View History...";
	this.objId = objId;
}

ViewHistoryAction.prototype.perform = function() {
	var url = BasePath.get() + "/livcos.org/web/history?ref=" + this.objId + "&view="
		+ encodeURIComponent(document.location);
	window.open(url);
}

ViewHistoryAction.prototype.close = function() {
}

function ViewExtHistoryAction(objId) {
	this.id = "viewHistory";
	this.label = "View History (ExtJS)...";
	this.objId = objId;
}

ViewExtHistoryAction.prototype.perform = function() {
	var url = BasePath.get() + "/livcos.org/web/ext/history?ref=" + this.objId + "&view="
		+ encodeURIComponent(document.location);
	window.open(url);
}

ViewExtHistoryAction.prototype.close = function() {
}

function ViewCosmosBrowserAction(objId) {
	this.id = "viewCosmosBrowser";
	this.label = "View Cosmos Browser...";
	this.objId = objId;
}

ViewCosmosBrowserAction.prototype.perform = function() {
	var url = BasePath.get() + "/livcos.org/web/admin/explore.view";
	if (this.objId) {
		var oId = "";
		var idx = this.objId.lastIndexOf("/");
		if (0 < idx)
			oId = this.objId.substring(0, idx);
		url += "?objId=/cosmos" + oId;
	}
	window.open(url);
}

ViewCosmosBrowserAction.prototype.close = function() {
}

function EditViewAction(viewObjId) {
	this.id = "editView";
	this.label = "Edit View...";
	this.viewObjId = viewObjId;
}

EditViewAction.prototype.perform = function() {
	if (!this.viewObjId && this.entityView)
		this.viewObjId = this.entityView.getViewObjId();
	var url = BasePath.get() + "/livcos.org/web/data.view?ref=" + this.viewObjId;
	window.open(url);
}

EditViewAction.prototype.close = function() {
}
